package com.opennews.openplatform.service;

import com.opennews.openplatform.dto.OrderDto;
import lombok.RequiredArgsConstructor;
import org.apache.seata.spring.annotation.GlobalTransactional;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@RequiredArgsConstructor
@Service
public class OrderService {
    private static final String TOPIC = "order-topic";

    private final KafkaTemplate<String, Object> kafkaTemplate;

    public List<Map<String, Object>> list() {
        return List.of(
                Map.of(
                        "id", "3d78c8afec8443568ba93d65464e6fe6",
                        "title", "order-1",
                        "productId", "2d78c8afec844356868ba93d"
                ),
                Map.of(
                        "id", "3d78c8afec8443568ba93d65464e6fe7",
                        "title", "order-2",
                        "productId", "2d78c8afec844356868ba94d"
                )
        );
    }

    @GlobalTransactional
    //    @Transactional
    public void testTransaction() {
        //        dslContext.update(BANK)
        //                .set(BANK.SHORT_NAME, BANK.SHORT_NAME.concat("-3"))
        //                .where(BANK.ID.eq("402881396f36d34b016f36d3984c0001"))
        //                .execute();
    }

    public void placeOrder() {
        final OrderDto orderDto = new OrderDto();
        orderDto.setId("40288");
        orderDto.setDateCreated(LocalDateTime.now());

        // Sends message to Kafka server.
        kafkaTemplate.send(TOPIC, orderDto);
        System.out.println("Message sent: " + orderDto);
    }
}
